package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/routers/service/walletService"
	"hc_server/runtime/e"
)

type WalletListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
}

// WalletList 钱包详细列表
func WalletList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   WalletListParam
		errCode int
	)

	_, errCode = app.BindAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	wallet := walletService.Wallet{
		AgentId:  cast.ToInt(userId),
		PageSize: cast.ToInt(param.PageSize),
		PageNum:  cast.ToInt(param.PageNum),
	}
	result, err := wallet.WalletList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
	}

	appG.ResponseSuccess("成功", result)
}

// WalletApplyList 申请提现列表
func WalletApplyList(c *gin.Context) {
	appG := app.Gin{C: c}

	userId, _ := c.Get("userId")

	wallet := walletService.Wallet{
		AgentId: cast.ToInt(userId),
	}
	result, err := wallet.WalletApplyList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
	}

	appG.ResponseSuccess("成功", result)
}

// WalletWithdrawalRecord 提现记录
func WalletWithdrawalRecord(c *gin.Context) {
	appG := app.Gin{C: c}

	userId, _ := c.Get("userId")

	wallet := walletService.Wallet{
		AgentId: cast.ToInt(userId),
	}
	result, err := wallet.WalletWithdrawalRecord()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
	}

	appG.ResponseSuccess("成功", result)
}

type WalletWithdrawalParam struct {
	Ids      any `json:"ids"`
	Amount   any `json:"amount"`
	BankName any `json:"bankName"`
	BankNo   any `json:"bankNo"`
}

// WalletWithdrawal 立即提现
func WalletWithdrawal(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   WalletWithdrawalParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	wallet := walletService.Wallet{
		AgentId:     cast.ToInt(userId),
		Ids:         cast.ToString(param.Ids),
		AfterAmount: cast.ToFloat32(param.Amount),
		BankName:    cast.ToString(param.BankName),
		BankNo:      cast.ToString(param.BankNo),
	}
	err := wallet.WalletWithdrawal()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", nil)
}
